package view.user;


import common.RoomTypes;
import model.booking.Booking;
import model.room.Room;
import model.room.RoomCount;
import model.user.User;
import view.login.Login;

import java.awt.*;
import java.awt.event.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.time.LocalDate;
import javax.swing.*;
/*
 * Created by JFormDesigner on Tue May 28 15:24:46 HKT 2024
 */



/**
 * @author 11565
 */
public class Mainframe extends JFrame {
    
    private User user;
    private LocalDate startTime;
    private LocalDate endTime;
    private RoomTypes roomTypes;

    public Mainframe(User user) {

        initComponents();
        this.user = user;
        label1.setText("                               欢迎光临：" + user.getName());
        TimePanel tp = new TimePanel(this);
        RoomTypePanel roomTypePanel = new RoomTypePanel(this);
        ReservationHistoryPanel rhPanel = new ReservationHistoryPanel(user);
        PersonalInformationPanel plpPanel = new PersonalInformationPanel(this,user);
        FillInInformationPanel fillInInformationPanel = new FillInInformationPanel(user,this);
        EditPanel editPanel = new EditPanel(this,user);
        mainpanel.add(editPanel,"editpanel");
        mainpanel.add(rhPanel, "rhPanel");
        mainpanel.add(plpPanel, "plpPanel");
        mainpanel.add(tp,"timepanel");
        mainpanel.add(roomTypePanel,"roomtypepanel");
        mainpanel.add(fillInInformationPanel,"fillininformationpanel");
    }

    public void showTimePanel()
    {
        CardLayout cl = (CardLayout)mainpanel.getLayout();
        cl.show(mainpanel,"timepanel");
    }
    public void showRoomType(LocalDate startTime,LocalDate endTime) {
        this.startTime = startTime;
        this.endTime = endTime;
        CardLayout cl = (CardLayout)mainpanel.getLayout();
        cl.show(mainpanel,"roomtypepanel");
    }
    public void showFillInInformationPanel(RoomTypes roomTypes) {
        this.roomTypes = roomTypes;
        CardLayout cl = (CardLayout)mainpanel.getLayout();
        cl.show(mainpanel,"fillininformationpanel");
    }

    void showUserInfoPanel(){
        CardLayout cl = (CardLayout)mainpanel.getLayout();
        cl.show(mainpanel,"plpPanel");
    }
    void showEditPanel(){
        CardLayout cl = (CardLayout)mainpanel.getLayout();
        cl.show(mainpanel,"editpanel");
    }
    private void BookingButton1(ActionEvent e) {
        // TODO add your code here
        showTimePanel();
    }

    private void UserButton(ActionEvent e) {
        // TODO add your code here
        showUserInfoPanel();
    }

    private void BookingHostry(ActionEvent e) {
        // TODO add your code here
        CardLayout cl = (CardLayout)mainpanel.getLayout();
        cl.show(mainpanel,"rhPanel");
    }

    private void UserExitbutton(ActionEvent e) {
        // TODO add your code here
        //更新Bookings文件

        //将Rooms集合保存在指定文件中
        String filename = "project/src/common/Bookings.txt";  //文件路径
        try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename))) {
            out.writeObject(Booking.getBookingList());
        } catch (IOException x) {
            x.printStackTrace();
        }
        //退出
        new Login().setVisible(true);
        this.dispose();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        BookingButton1 = new JButton();
        UserButton = new JButton();
        BookingHostryButton = new JButton();
        mainpanel = new JPanel();
        label1 = new JLabel();
        button1 = new JButton();

        //======== this ========
        var contentPane = getContentPane();

        //---- BookingButton1 ----
        BookingButton1.setText("\u623f\u95f4\u9884\u7ea6");
        BookingButton1.addActionListener(e -> BookingButton1(e));

        //---- UserButton ----
        UserButton.setText("\u4e2a\u4eba\u4e2d\u5fc3");
        UserButton.addActionListener(e -> UserButton(e));

        //---- BookingHostryButton ----
        BookingHostryButton.setText("\u9884\u7ea6\u8bb0\u5f55");
        BookingHostryButton.addActionListener(e -> BookingHostry(e));

        //======== mainpanel ========
        {
            mainpanel.setLayout(new CardLayout());

            //---- label1 ----
            label1.setText("text");
            mainpanel.add(label1, "card1");
        }

        //---- button1 ----
        button1.setText("\u9000\u51fa");
        button1.addActionListener(e -> UserExitbutton(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(17, 17, 17)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                        .addComponent(BookingButton1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(UserButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(BookingHostryButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(button1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(59, 59, 59)
                    .addComponent(mainpanel, GroupLayout.PREFERRED_SIZE, 608, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(37, 37, 37)
                            .addComponent(BookingButton1)
                            .addGap(40, 40, 40)
                            .addComponent(UserButton)
                            .addGap(36, 36, 36)
                            .addComponent(BookingHostryButton)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(button1, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(mainpanel, GroupLayout.PREFERRED_SIZE, 390, GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(23, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JButton BookingButton1;
    private JButton UserButton;
    private JButton BookingHostryButton;
    private JPanel mainpanel;
    private JLabel label1;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on


    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public LocalDate getStartTime() {
        return startTime;
    }

    public void setStartTime(LocalDate startTime) {
        this.startTime = startTime;
    }

    public LocalDate getEndTime() {
        return endTime;
    }

    public void setEndTime(LocalDate endTime) {
        this.endTime = endTime;
    }

    public RoomTypes getRoomTypes() {
        return roomTypes;
    }

    public void setRoomTypes(RoomTypes roomTypes) {
        this.roomTypes = roomTypes;
    }
}
